package com.czy.seed.mvc.itextPdf;

import com.czy.seed.mvc.home.entity.PurchaseOrderDetailVo;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class CreatePdf_MemorandInUniting {
    public void creatememorand(String url, String path, String quote_unit, String name, String lastTime, List<PurchaseOrderDetailVo> list, String jhdd, String shr, String dxrmb) throws FileNotFoundException, DocumentException, IOException {

        File file = new File(url);
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
        Float f = 25f;
        // 创建文件
        Document document = new Document();
        document.setPageSize(PageSize.A4);
        //建立一个书写器
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
        try {
            //打开文件
            document.open();

            //中文字体,解决中文不能显示问题
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font fontChar = new Font();
            fontChar.setSize(7);
            fontChar.setColor(BaseColor.DARK_GRAY);
            //黑色字体
            Font blueFont = new Font(bfChinese);
            blueFont.setColor(BaseColor.BLACK);
            //段落文本
            blueFont.setSize(18);
            Paragraph paragraphBlue = new Paragraph("张掖市市级单位政府采购协议供货和定点采购格式合同", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            blueFont.setSize(10);
            paragraphBlue = new Paragraph("合同编号：20     (年份)协或定                        (需方简称)                (序号)：", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("合同备案号：20     (年份)协或定备                        (需方简称)                (序号)：", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            blueFont.setSize(14);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("供方：", blueFont);
            Chunk tChunk2 = new Chunk("                                                                           ", blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("需方：", blueFont);
            tChunk2 = new Chunk("                                                                           ", blueFont);
            tChunk2.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);


            paragraphBlue = new Paragraph("为了保护供需双方合法权益，根据《中华人民共和国政府采购法》、《中华人民共", blueFont);
            paragraphBlue.setIndentationLeft(16); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("和国合同法》等相关法律规定以以及政府采购协议供货和定点采购招标文件、政府采购协议供货和定点采购承诺书中的相关规定，签订本合同，并共同遵守。", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("", blueFont);
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            blueFont.setStyle(Font.BOLD);
            tChunk2 = new Chunk(" 一、采购品目、型号、数量及其金额", blueFont);
            paragraphBlue.add(tChunk2);
            blueFont.setStyle(Font.UNDEFINED);
            tChunk2 = new Chunk(" （也可另附供货清单）", blueFont);
            paragraphBlue.add(tChunk2);
            document.add(paragraphBlue);
            //处理表格
            document.add(Chunk.NEWLINE); // 新建一行
            blueFont.setSize(10);
            //添加表格
            PdfPTable table = new PdfPTable(new float[]{80, 80, 220, 60, 60, 60, 30, 30, 30, 30, 30, 30, 30, 30});// 8列的表格以及单元格的宽度。
            table.setTotalWidth(900);//表格整体宽度
            PdfPCell cell;
            cell = new PdfPCell(new Phrase("", blueFont));
            cell.setColspan(1);//占据八列
            cell.setRowspan(1);
            cell.disableBorderSide(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("", blueFont));
            cell.setColspan(1);
            cell.setRowspan(1);
            cell.disableBorderSide(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("", blueFont));
            cell.setColspan(1);//占据八列
            cell.setRowspan(1);
            cell.disableBorderSide(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("数量", blueFont));
            cell.setColspan(1);//占据八列
            cell.setRowspan(1);
            cell.disableBorderSide(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("市场", blueFont));
            cell.setColspan(1);//占据八列
            cell.setRowspan(1);
            cell.disableBorderSide(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("成交", blueFont));
            cell.setColspan(1);//占据八列
            cell.setRowspan(1);
            cell.disableBorderSide(2);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("                      总价", blueFont));
            cell.setColspan(8);//占据八列
            cell.setRowspan(1);
            cell.disableBorderSide(2);
            table.addCell(cell);

            table.addCell(new Paragraph("    品目", blueFont));
            table.addCell(new Paragraph("    型号", blueFont));
            table.addCell(new Paragraph("            主要技术指标", blueFont));
            table.addCell(new Paragraph("（）", blueFont));
            table.addCell(new Paragraph("单价", blueFont));
            table.addCell(new Paragraph("单价", blueFont));
            table.addCell(new Paragraph("十", blueFont));
            table.addCell(new Paragraph("万", blueFont));
            table.addCell(new Paragraph("千", blueFont));
            table.addCell(new Paragraph("百", blueFont));
            table.addCell(new Paragraph("十", blueFont));
            table.addCell(new Paragraph("元", blueFont));
            table.addCell(new Paragraph("角", blueFont));
            table.addCell(new Paragraph("分", blueFont));
            int k = list.size();
            for (PurchaseOrderDetailVo ls : list) {
                table.addCell(new Paragraph(ls.getPmmc(), blueFont));
                table.addCell(new Paragraph(ls.getGgxh(), blueFont));
                table.addCell(new Paragraph(ls.getJscs(), blueFont));
                table.addCell(new Paragraph(ls.getNum() + "", blueFont));
                table.addCell(new Paragraph(ls.getCommodity_mak_prc(), blueFont));
                table.addCell(new Paragraph(ls.getDj() + "", blueFont));
                table.addCell(new Paragraph(ls.getSw(), blueFont));
                table.addCell(new Paragraph(ls.getW(), blueFont));
                table.addCell(new Paragraph(ls.getQ(), blueFont));
                table.addCell(new Paragraph(ls.getB(), blueFont));
                table.addCell(new Paragraph(ls.getS(), blueFont));
                table.addCell(new Paragraph(ls.getY(), blueFont));
                table.addCell(new Paragraph(ls.getJ(), blueFont));
                table.addCell(new Paragraph(ls.getF(), blueFont));
            }
            if (k < 5) {
                for (int l = 0; l <= 5 - k; l++) {
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph("  ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                    table.addCell(new Paragraph(" ", blueFont));
                }
            }
            cell = new PdfPCell(new Phrase("      合        计", blueFont));
            cell.setColspan(2);//占据八列
            cell.setRowspan(1);
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("大写（人民币：）" + dxrmb, blueFont));
            cell.setColspan(12);//占据八列
            cell.setRowspan(1);
            table.addCell(cell);
            document.add(table);
            blueFont.setSize(8);
            paragraphBlue = new Paragraph("注：市场单价为通过招标确定的入围品目的市场价格；成交单价为采购人与供应商及代理商在市场单价以内询价协商后确定的合同价格。", blueFont);
            paragraphBlue.setIndentationLeft(16); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            blueFont.setSize(14);
            paragraphBlue = new Paragraph("", blueFont);
            blueFont.setStyle(Font.BOLD);
            Chunk tChunk3 = new Chunk(" 二、保修期：", blueFont);
            paragraphBlue.add(tChunk3);
            blueFont.setStyle(Font.UNDEFINED);
            tChunk3 = new Chunk("产品的服务标准，与商品制造商中国总部对外公众网站上公布的标准相", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("一致；若投标文件承诺的服务优于网站公布标准或其他未公布标准的各类产品应依据投标文件中承诺的标准提供服务。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("", blueFont);
            blueFont.setStyle(Font.BOLD);
            tChunk3 = new Chunk(" 三、交（提）货时间、地点、方式：", blueFont);
            paragraphBlue.add(tChunk3);
            blueFont.setStyle(Font.UNDEFINED);
            tChunk3 = new Chunk("供方于", blueFont);
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("             " + lastTime + "  ", blueFont);
            tChunk3.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("前将货物送", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk3 = new Chunk("至", blueFont);
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("           " + jhdd + "                  ", blueFont);
            tChunk3.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("（指定地点）交由", blueFont);
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("            " + shr + "                    ", blueFont);
            tChunk3.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("（需方指定收货人）验收。此外，供方应在交货前向需方提供交货计划；运输、保险和装箱的费用由供方承担。", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("", blueFont);
            blueFont.setStyle(Font.BOLD);
            tChunk3 = new Chunk(" 四、验收标准及提出异议时间：", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);

            blueFont.setStyle(Font.UNDEFINED);

            paragraphBlue = new Paragraph("（一）验收标准：1、装箱单；2、合格证；3、供方保证一次性合格率大于98%（百分之 九十八）。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("（二）提出异议时间截止：", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk3 = new Chunk("                                      ", blueFont);
            tChunk3.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行


            paragraphBlue = new Paragraph("", blueFont);
            blueFont.setStyle(Font.BOLD);
            tChunk3 = new Chunk(" 五、付款方式及期限：", blueFont);
            paragraphBlue.add(tChunk3);

            blueFont.setStyle(Font.UNDEFINED);
            tChunk3 = new Chunk("双方签署验收单后，需方直接支付的，需方应在", blueFont);
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("           ", blueFont);
            tChunk3.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("个工作", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("日内支付；财政国库集中支付的，预算单位持相关资料及支付报告到张掖市财政局办理。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("供方开户行名称：", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            tChunk3 = new Chunk("                                        ", blueFont);
            tChunk3.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk(" ；账号：", blueFont);
            paragraphBlue.add(tChunk3);
            tChunk3 = new Chunk("                                        ", blueFont);
            tChunk3.setUnderline(0.2f, -2f); // 下划线
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);


            paragraphBlue = new Paragraph("", blueFont);
            blueFont.setStyle(Font.BOLD);
            tChunk3 = new Chunk("六、违约责任：", blueFont);
            paragraphBlue.add(tChunk3);
            blueFont.setStyle(Font.UNDEFINED);
            tChunk3 = new Chunk("1、如供方延期交货或需方延期付款，每逾期一日，违约方因按照合", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("同金额1%向对方支付违约金，但该违约金累计不超过合同金额5%，逾期超过5日，有权利解除合同。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);


            paragraphBlue = new Paragraph("", blueFont);
            blueFont.setStyle(Font.BOLD);
            tChunk3 = new Chunk("七、解决合同纠纷方式：", blueFont);
            paragraphBlue.add(tChunk3);

            document.add(paragraphBlue);
            blueFont.setStyle(Font.UNDEFINED);

            paragraphBlue = new Paragraph("双方协商                              □；        向授权厂商投诉              □；", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("向财政部门投诉                □；       提请仲裁                             □；", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph(" 向人民法院提起诉讼      □。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph(" 授权厂商投诉电话：", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("", blueFont);
            blueFont.setStyle(Font.BOLD);
            tChunk3 = new Chunk(" 八、其他约定事项：", blueFont);
            paragraphBlue.add(tChunk3);
            document.add(paragraphBlue);

            blueFont.setStyle(Font.UNDEFINED);

            paragraphBlue = new Paragraph("1、招标文件、投标文件和中标供应商承诺书是本合同不可分割的部分，对采购人和中标供应商均具有法律效力，本合同未尽事宜从其规定。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("2、本合同由需方支付货款的经供需双方签字、盖章后生效；由财政国库支付货款的，经供需双方签字、盖章并经市财政局相关科室确认资金后生效。合同内容如遇国家法律规定及政策另有规定的，从其规定。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("3、本合同为供应商与采购人买卖合同，一式三份，供需双方各执一份，市政府采购办公室备案一份。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            document.add(Chunk.NEWLINE); // 新建一行

            blueFont.setStyle(Font.BOLD);
            paragraphBlue = new Paragraph("需方：（签章）                                               供方（签章）", blueFont);
            paragraphBlue.setIndentationLeft(64); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("地址：                                                                   地址：", blueFont);
            paragraphBlue.setIndentationLeft(64); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("电话：                                                                   电话：", blueFont);
            paragraphBlue.setIndentationLeft(64); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);
            paragraphBlue = new Paragraph("代表：                                                                   代表：", blueFont);
            paragraphBlue.setIndentationLeft(64); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            blueFont.setStyle(Font.UNDEFINED);
            document.add(Chunk.NEWLINE); // 新建一行
            paragraphBlue = new Paragraph("                                               合同签订日期：                  年         月         日", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            paragraphBlue = new Paragraph("注：本合同为格式合同，供需双方可根据实际情况修订。", blueFont);
            paragraphBlue.setIndentationLeft(32); //在段落左边插入175个空格
            paragraphBlue.setAlignment(Element.ALIGN_LEFT);
            paragraphBlue.setLeading(f);
            document.add(paragraphBlue);

            //关闭文档
            document.close();
            //关闭书写器
            writer.close();

            ZipCompress zipCom = new ZipCompress(path.replace(".pdf", ".zip"), path);
            try {
                zipCom.zip();
            } catch (Exception e) {
                e.printStackTrace();
            }


        } catch (Exception e) {
            throw e;
        } finally {
            if (writer != null) {
                writer.close();
            }
            if (document != null) {
                document.close();
            }
        }

    }
}
